package mao.leetcode.offer;

import java.util.HashSet;

/**
 * @author Totoro
 * @date 2020/8/7
 * @explain:
 */
public class interview48 {

    public int lengthOfLongestSubstring(String s) {
        if (s == null || s.equals("")) return 0;
        int left = 0,right = 0;
        int maxWin = 0,len=s.length();
        HashSet<Character> set = new HashSet<>();
        while(right < len){
            while(right < len && !set.contains(s.charAt(right))){

                set.add(s.charAt(right));
                right++;
                maxWin = Math.max(maxWin,right-left);
            }
            if (right >= len) break;
            for(;left<right;left++){
                set.remove(s.charAt(left));
                if (s.charAt(left) == s.charAt(right)){
                    left++;
                    break;
                }
            }
        }
        return maxWin;
    }
}
